package xde2d.parser
{
	import flash.geom.Vector3D;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import away3d.core.math.Quaternion;
	import away3d.loaders.parsers.ParserBase;
	import away3d.loaders.parsers.ParserDataFormat;
	
	import xde2d.utils.Matrix3DUtils;

	/**
	 *  @author:Gaara
	 *  2012-12-21
	 *  二进制读取基类
	 **/
	public class BinaryParser extends ParserBase
	{
		/** load的数据 **/
		public var _startedParsing : Boolean;
		
		protected var _line:int;
		protected var _charLineIndex:int;
		private var _parseIndex:int;
		
		protected var _byteData:ByteArray;
		
		/** 是否读到结尾 **/
		protected var _reachedEOF:Boolean;
		
		public function BinaryParser()
		{
			super(ParserDataFormat.BINARY);
		}
		
		/** 读取的数据 **/
		public function get byteData():ByteArray
		{
			if(!_byteData){
				_byteData = getByteData();
				_byteData.endian = Endian.LITTLE_ENDIAN;
			}
			return _byteData;
		}

		protected function getNextInt() : int
		{
			var value:int = byteData.readInt();
			if (isNaN(value))
			{
				this.sendParseError("int type");
			}
			return value;
		}
		
		protected function getNextBool() : Boolean
		{
			var bool:Boolean = byteData.readBoolean();
			return bool;
		}
		
		protected function getNextNumber() : Number
		{
			var value:Number = byteData.readFloat();
			if (isNaN(value))
			{
				this.sendParseError("float type");
			}
			return value;
		}
		
		protected function parseVector3D() : Vector3D
		{
			var _loc_1:* = new Vector3D();
			_loc_1.x = this.getNextNumber();
			_loc_1.y = this.getNextNumber();
			_loc_1.z = this.getNextNumber();
			return _loc_1;
		}
		
		protected function parseScale() : Vector3D
		{
			var _loc_1:* = new Vector3D();
			_loc_1.x = this.getNextNumber();
			_loc_1.y = this.getNextNumber();
			_loc_1.z = this.getNextNumber();
			if (_loc_1.x < 1e-005)
			{
				_loc_1.x = 1e-005;
			}
			if (_loc_1.y < 1e-005)
			{
				_loc_1.y = 1e-005;
			}
			if (_loc_1.z < 1e-005)
			{
				_loc_1.z = 1e-005;
			}
			return _loc_1;
		}
		protected function parseQuaternion() : Quaternion
		{
			var _loc_1:* = Matrix3DUtils.tempQuat;
			_loc_1.x = getNextNumber();
			_loc_1.y = getNextNumber();
			_loc_1.z = getNextNumber();
			var _loc_2:* = 1 - _loc_1.x * _loc_1.x - _loc_1.y * _loc_1.y - _loc_1.z * _loc_1.z;
			_loc_1.w = _loc_2 < 0 ? (0) : (-Math.sqrt(_loc_2));
			var _loc_3:* = Matrix3DUtils.getMaxToGameRotation();
			_loc_1.multiply(_loc_3, _loc_1);
			return _loc_1;
		}
		
		/** 读取字符串 **/
		protected function getNextString() : String
		{
			var len:uint = this.getNextInt();
			return byteData.readMultiByte(len, "gb2312");
		}
		
		protected function sendEOFError() : void
		{
			throw new Error("Unexpected end of file");
		}
		
		/**抛出一个解析错误 **/
		protected function sendParseError(content:String) : void
		{
			throw new Error("Unexpected token at line " + (this._line + 1) + ", character " + this._charLineIndex + ". " + content + " expected, ");
		}
		
		protected function sendUnknownKeywordError() : void
		{
			throw new Error("Unknown keyword at line " + (this._line + 1) + ", character " + this._charLineIndex + ". ");
		}
	}
}